package com.rocpenghua.excel.easyexcel;

import com.alibaba.excel.EasyExcel;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author pengp
 * @date 2024年02月27日 10:47
 */
public class TestMain {
    public static List<List<String>> data2() {
        List<List<String>> data = new ArrayList<>(10);
        List<String> row = new ArrayList<>();
        row.add("20220225001126");
        row.add("二级");
        row.add("已关闭");
        row.add("事中管控");
        row.add("20220216001022");
        row.add("2022-01-13");
        row.add("业扩报装");
        row.add("事中测试1");
        row.add(null);
        data.add(row);
        return data;
    }


    private static String getRondomNumbers(){
        int number = RandomUtils.nextInt(0, 100);
        return String.valueOf(number) ;
    }

    public static void main(String[] args) {
        String fileName = TestMain.class.getResource("/").getPath()+ "mul-head" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(fileName)
//                .registerWriteHandler(new WaterMarkHandler("张三,2023,31231312"))
                .head(headMerge())
                .registerWriteHandler(new DefaultCellWriteWidthConfig())
                .registerWriteHandler(new CustomCellWriteHeightConfig())
                .registerWriteHandler(EasyDataUtil.getStyleStrategy())
                .sheet("sheet").doWrite(data2());

        //补充数据
//        List<List<String>> titleList = new ArrayList<>();
//        List<String> titleOne = Lists.newArrayList("统计","","","","");
//        List<String> titleTwo = Lists.newArrayList("数值","","","比例","");
//        titleList.add(makeUpTitle(titleOne));
//        titleList.add(makeUpTitle(titleTwo));
//        System.out.println("make up title: "+titleList.toString() );

//        List<List<String>> titleList = new ArrayList<>();
//        List<String> titleOne = Lists.newArrayList("统计","","","","");
//        List<String> titleTwo = Lists.newArrayList("数值","","","比例","");
//        titleList.add(makeUpTitle(titleOne));
//        titleList.add(makeUpTitle(titleTwo));
//        List<List<String>> roateHeadFields = roateHeadFields(titleList);
//        System.out.println("roateHeadFields: "+roateHeadFields.toString());
    }

    private static List<List<String>> roateHeadFields(List<List<String>> headFields) {
        List<List<String>> result = new ArrayList<>();
        for (List<String> row : headFields) {
            for (int j = 0; j < row.size(); j++) {
                if (result.size() > j) {
                    // 往对应第j个List<String> 添加添加值
                    result.get(j).add(row.get(j));
                } else {
                    // 分割成单个List<String>
                    result.add(new ArrayList<>(Collections.singletonList(row.get(j))));
                }
            }
        }
        return result;
    }
    private static List<List<String>> headMerge() {
        List<List<String>> list = new ArrayList<List<String>>();
        String total = "统计";
        List<String> head0 = new ArrayList<String>();
        head0.add("督办单督办单单号督办单单号单号");
        List<String> head1 = new ArrayList<String>();
        head1.add("督办等级");
        List<String> head2 = new ArrayList<String>();
        head2.add("进度");
        List<String> head3 = new ArrayList<String>();
        head3.add("任务类型");
        List<String> head4 = new ArrayList<String>();
        head4.add("任务编号");
        List<String> head5 = new ArrayList<String>();
        head5.add("要求完成时间");
        list.add(head0);
        list.add(head1);
        list.add(head2);
        list.add(head3);
        list.add(head4);
        list.add(head5);
        return list;
    }

    private static List<String> makeUpTitle(List<String> titleList){
        List<String> makeUpList = new ArrayList<>();
        String temp = "";
        for (String e : ListUtils.emptyIfNull(titleList)) {
            if (StringUtils.isNotEmpty(e)) {
                temp = e;
            }
            makeUpList.add(temp);
        }

        return makeUpList;
    }
}
